package dongchel.vrp.clusterandroute;

import java.util.ArrayList;
import java.util.List;

import dongchel.vrp.entity.City;
import dongchel.vrp.entity.PDCity;
import dongchel.vrp.entity.Route;
import dongchel.vrp.entity.Solution;
import dongchel.vrp.general.vrpinterface.ObjectivePenaltyInterface;
import dongchel.vrp.rachelinsertion.Insertion;
import dongchel.vrp.rachelinsertion.InsertionObject;

public class Cluster {
	/**
	 * Unvisited cityList Route Method: Add Remove
	 */
	private Solution solution;
	private List<City> cityList = new ArrayList<City>();

	private ObjectivePenaltyInterface objectivePanelty;
	private Insertion insertion;

	public Cluster(Solution solution) {
		this.solution = solution;
		this.objectivePanelty = solution.getObjectivePenalty();
		this.insertion = solution.getInsertion();
	}

	public Route getRouteFromCluster() {
		Route route = new Route(solution);
		while (cityList.size() > 0) {
			int index = (int) Math.floor(Math.random() * cityList.size());
			City city = cityList.get(index);
			insertion.insertionCityintoRoute(route, city, route.getCityList()
					.size()-1);
			cityList.remove(city);
		}
		return route;
	}
	
	public Route getRouteFromClusterByNearestNeighbour(){
		Route route = new Route(solution);
		while (cityList.size() > 0) {
			InsertionObject best = null;
			for(int i=0;i<cityList.size();i++){
				City city = cityList.get(i);
				InsertionObject current = insertion.getInsertionObject(route, city, route.getCityList().size()-1);
				if(best == null){
					best = current;
				}else{
					if(current.compare(best)<0){
						best = current;
					}
				}				
			}
			
			if(best != null){
				insertion.insertionCityintoRoute(route, best.getCity(), best.getPosition());
				cityList.remove(best.getCity());
			}
			
		}
		return route;
	}

	public List<City> getCityList() {
		return cityList;
	}

	public void setCityList(List<City> cityList) {
		this.cityList = cityList;
	}

}
